package com.allinfinance.starters.xml.loader.config;

import com.allinfinance.starters.interfaces.StringConfigLoader;
import com.allinfinance.starters.loader.DbConfigLoader;
import com.allinfinance.starters.loader.FileConfigLoader;
import com.allinfinance.starters.loader.MultipartLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: 简单流程自动配置类
 * @author: wujp
 * @create: 2025-01-02 19:17
 */
@EnableConfigurationProperties(XmlLoaderAutoProperties.class)
public class XmlLoaderAutoConfig {

    @Autowired
    private XmlLoaderAutoProperties xmlLoaderAutoProperties;

    @Autowired(required = false)
    private DataSource dataSource;

    @Autowired
    private ApplicationContext applicationContext;

    @ConditionalOnClass(DataSource.class)
    @ConditionalOnProperty(name = "xml.loader.hasDbResources", havingValue = "true")
    @Bean("xmlDbConfigLoader")
    public StringConfigLoader xmlDbConfigLoader() {
        DbConfigLoader dbConfigLoader = new DbConfigLoader();
        dbConfigLoader.setDataSource(dataSource);
        dbConfigLoader.setConfigTableName(xmlLoaderAutoProperties.getConfigTableName());
        dbConfigLoader.setConfigKeyFieldName(xmlLoaderAutoProperties.getConfigKeyFieldName());
        dbConfigLoader.setConfigValueFieldName(xmlLoaderAutoProperties.getConfigValueFieldName());
        return dbConfigLoader;
    }

    @ConditionalOnProperty(name = "xml.loader.localFileResources")
    @Bean("xmlFileConfigLoader")
    public StringConfigLoader xmlFileConfigLoader() {
        FileConfigLoader fileConfigLoader = new FileConfigLoader();
        fileConfigLoader.setFileEncoding(xmlLoaderAutoProperties.getFileEncoding());
        fileConfigLoader.setNote(xmlLoaderAutoProperties.getNote());
        fileConfigLoader.setSuffix(xmlLoaderAutoProperties.getSuffix());
        fileConfigLoader.setLocalFileResources(xmlLoaderAutoProperties.getLocalFileResources());
        return fileConfigLoader;
    }

    @Bean("xmlMultipartLoader")
    public StringConfigLoader xmlMultipartLoader() {
        MultipartLoader multipartLoader = new MultipartLoader();
        List<StringConfigLoader> loaders = new ArrayList<>();
        if (applicationContext.containsBean("xmlDbConfigLoader")) {
            loaders.add(applicationContext.getBean("xmlDbConfigLoader", StringConfigLoader.class));
        }
        if (applicationContext.containsBean("xmlFileConfigLoader")) {
            loaders.add(applicationContext.getBean("xmlFileConfigLoader", StringConfigLoader.class));
        }
        multipartLoader.setLoaders(loaders);
        return multipartLoader;
    }

}
